import mitt from 'mitt';
import {VCairnEvent} from "../vcc/VCairnEvent";

const emitter = mitt();

export class EventDispatcher {
    private static _instance:EventDispatcher;
    public static getInstance() {
        if (!EventDispatcher._instance) {
            EventDispatcher._instance = new EventDispatcher();
        }
        return EventDispatcher._instance;
    }

    on(type:string, listener:(evt:any) => void) {
        emitter.on(type, listener);
    }

    emit(event:VCairnEvent) {
        emitter.emit(event.type, event);
    }

    off(type:string, listener: (evt:any) => void) {
        emitter.off(type, listener);
    }

    hasOn(type:string) {
        return emitter.all.has(type) && !!emitter.all.get(type)?.length;
    }
}

export default EventDispatcher.getInstance();